package com.sgu6.sgu6_machine.ui.activity;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.MenuItem;
import android.widget.EditText;

import com.sgu6.middleware.manager.ThreadManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseA;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.event.ExitTimeEvent;
import com.sgu6.sgu6_machine.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExitTimeA extends BaseA {

    AlertDialog.Builder dialog;
    String exitTime;

    Thread thread;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_exit_time)
    EditText etExitTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exit_time);
        ButterKnife.bind(this);

        initUI();
    }

    @Override
    public void initUI() {
        initToolbar();
        initDialog();
    }

    private void initDialog() {
        dialog = new AlertDialog.Builder(this);
        dialog.setTitle("提示");
        dialog.setMessage("是否修改退出时间？");
        dialog.setNegativeButton("取消", (dialog, which) -> {
        });
        dialog.setPositiveButton("确定", (dialog, which) -> {
            ThreadManager.getThreadPollProxy().execute(() -> {
                MachineContainer.machineConfigDto.exitSetTime = Integer.parseInt(exitTime);
                EventBus.getDefault().post(new ExitTimeEvent(true, Integer.parseInt(exitTime)));
                runOnUiThread(() -> {
                    ToastUtil.show(ExitTimeA.this, "退出时间修改成功");
                    ExitTimeA.this.finish();
                });
                updateConfig();
            });
        });
    }

    private void initToolbar() {
        toolbar.setTitle("自定义退出时间");
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setBackgroundResource(R.color.colorPrimaryDark);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        exitTime = etExitTime.getText().toString().trim();
        if (exitTime.length() > 0) {
            dialog.show();
        } else {
            ToastUtil.show(this, "请填写完整信息");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (thread != null) {
            thread = null;
        }
    }

}
